package firstline.example.andy.notebook;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;


public class MainActivity extends Activity implements View.OnClickListener, AdapterView.OnItemClickListener {

    private Button edit;
    private DBhelper dBhelper;
    private SQLiteDatabase db;
    private Bean bean;
    private ArrayAdapter adapter;
    private ListView listView;
    private String[] cols;
    private Cursor cs;
    private String retitle;
    private ArrayList<String> lists,listscontent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        Log.d("onCreate","onCreate");
        initView();
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d("onStart", "onStart");
    }

    private void initView() {
        bean=new Bean();
        listView= (ListView) findViewById(R.id.listView);
        edit = (Button) findViewById(R.id.edit);
        edit.setOnClickListener(this);
        dBhelper=new DBhelper(this,"liuyanban.db");
        db=dBhelper.getWritableDatabase();
        cs=db.rawQuery("select *from liuyan", null);
        lists=new ArrayList<>();
        listscontent=new ArrayList<>();
        if (cs!=null){
            cols=cs.getColumnNames();
            while(cs.moveToNext()){
               lists.add(cs.getString(cs.getColumnIndex("title")));
               listscontent.add(cs.getString(cs.getColumnIndex("content")));
            }
            cs.close();
        }
        db.close();
        adapter=new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,lists);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
    }

    @Override
    public void onClick(View view) {
        Intent intent=new Intent(MainActivity.this,Edit.class);
        startActivityForResult(intent,0);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Intent intent=new Intent(this,Detail.class);
        Log.d("lists",lists.get(i));
        intent.putExtra("title",lists.get(i));
        intent.putExtra("content",listscontent.get(i));
        startActivity(intent);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==1) {
            Log.d("进来onActivity", "进来onActivity");
            retitle=data.getExtras().getString("title");
            lists.add(retitle);
            adapter.notifyDataSetChanged();
        }
    }
}
